using AutoMapper;
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Application.UserApp;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Core.SimpleTemp.Mvc.Api.Sys
{
    /// <summary>
    /// CBS_List
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class CBS_ListController : CoreApiController
    {
        private readonly CBS_ListService _service;
        private readonly SysUserAppService _userService;
        /// <summary>
        /// CBS_List
        ///</summary>
        public CBS_ListController(CBS_ListService service, SysUserAppService userService)
        {
            _service = service;
            _userService = userService;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        [PermissionFilter(nameof(CBS_ListController) + "_" + nameof(CBS_ListController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<CBS_ListDto>
                pagingQueryModel)
        {
            IPageModel<CBS_ListDto>
                result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }
        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(CBS_ListController) + "_" + nameof(CBS_ListController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(CBS_ListDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                //await _service.UpdateAsync(dto, true, new List<string>() { nameof(CBS_ListAttachment.SysUser) });
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(CBS_ListController) + "_" + nameof(CBS_ListController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            CBS_ListDto cBS_ListDto = Mapper.Map<CBS_ListDto>(model);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);

            //当前登陆用户信息
            var user = await _userService.GetAsync(UserIdentity.Id);
            //返回结果
            var result = new
            {
                SysUser = new
                {
                    id = user.Id,
                    name = user.SysUserInfor.Name
                },
                //cbs_list = model
                cbs_list = cBS_ListDto
            };
            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(CBS_ListController) + "_" + nameof(CBS_ListController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult> Export(PagingModel<CBS_ListDto> pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }

        [HttpPost("Import")]
        [PermissionFilter(nameof(CBS_ListController) + "_" + "导入")]
        public async Task<IActionResult> ImportAsync([FromForm]List<IFormFile> file)
        {

            var retMeg = await _service.ImportAsync(file[0]);
            if (!retMeg.Any())
                return this.JsonSuccess();
            else
                return this.JsonFaild(null, retMeg);
        }
        #endregion
    }
}
